﻿Imports System.Collections.Generic
Imports System.Linq
Partial Class Login
    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim flag As Boolean = False

        If txtUserName.Text = "" Then
            flag = True
            lblErrMessage.Text = "Invalid username or password"
            Exit Sub
        End If

        If txtPassword.Text = "" Then
            flag = True
            lblErrMessage.Text = "Invalid username or password"
            Exit Sub
        End If

        Dim SQLDB As New OneCall.DataModel.OneCDataEntities
        Dim Customer As List(Of OneCall.DataModel.Customer) = (From p In SQLDB.Customers Where p.Customer_UserName = txtUserName.Text).ToList()

        If (Not Customer Is Nothing) And (Customer.Count > 0) Then
            If Customer(0).Customer_Password = txtPassword.Text Then
                lblErrMessage.Text = ""
                If Customer(0).Customer_AcStatus = 1 Then
                    Dim cCust As New OneCall.User.AuthUser(Response)
                    cCust.RegisterUser(Customer(0).Customer_ID)
                    Customer = Nothing
                    SQLDB.Dispose()
                    Exit Sub
                Else
                    lblErrMessage.Text = "Acoount is not verified."
                End If
                
            Else
                flag = True
                lblErrMessage.Text = "Invalid username or password"
                Customer = Nothing
                SQLDB.Dispose()
                Exit Sub
            End If
        Else
            flag = True
            lblErrMessage.Text = "Invalid username or password"
            Customer = Nothing
            SQLDB.Dispose()
            Exit Sub
        End If

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim redo As String = Request("do")
        If redo = "out" Then
            Dim cUser As New OneCall.User.AuthUser(Request, Response)
            If cUser.isActiveSession Then
                cUser.SignOut()
                Response.Redirect("Login.aspx")
            Else
                Response.Redirect("Login.aspx")
            End If
        End If

    End Sub
End Class
